﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using StreetlightGames.XnaEngine;

namespace StreetlightGames.SpaceShooter
{
    public class LevelDisplay : DrawableGameComponent
    {
        private Model _singleLevelShield;
        private Model _fiveLevelShield;
        private List<ModelObject> _levelShields = new List<ModelObject>();

        /// <summary>Gets or sets the Camera value</summary>
        public ICamera Camera
        {
            get { return _camera; }
            set { _camera = value; }
        }
        private ICamera _camera;

        public LevelDisplay(Game game)
            : base(game)
        {
        }

        protected override void LoadContent()
        {
            _singleLevelShield = Game.Content.Load<Model>("Models\\GalagaLevelShield");
            _fiveLevelShield = Game.Content.Load<Model>("Models\\GalagaLevelShield5");
            base.LoadContent();
        }

        public void LoadLevels(SpaceShooterGame game)
        {
            _levelShields.Clear();
            float x = 500;
            for (int i = 0; i < game.Level % 5; i++)
            {
                ModelObject levelShield = new ModelObject(game, _singleLevelShield);
                levelShield.Position = new Vector3(x, 0, 50);
                levelShield.Scale = 5;
                levelShield.Camera = Camera;
                _levelShields.Add(levelShield);
                x -= 20;
            }
            for (int i = 0; i < game.Level / 5; i++)
            {
                ModelObject levelShield = new ModelObject(game, _fiveLevelShield);
                levelShield.Position = new Vector3(x - 40 * i, 0, 50);
                levelShield.Scale = 5;
                levelShield.Camera = Camera;
                _levelShields.Add(levelShield);
            }
        }

        public override void Update(GameTime gameTime)
        {
            SpaceShooterGame spaceShooterGame = Game as SpaceShooterGame;
            for (int i = 0; i < _levelShields.Count; i++)
            {
                ModelObject levelShield = _levelShields[i];
                levelShield.Position = Game.GraphicsDevice.Viewport.Unproject(
                                   new Vector3(
                                       Game.GraphicsDevice.Viewport.Width - 40 - 20 * i,
                                       Game.GraphicsDevice.Viewport.Height - 40,
                                       0.999f),
                                   spaceShooterGame.Camera.ProjectionMatrix,
                                   spaceShooterGame.Camera.ViewMatrix,
                                   Matrix.Identity);
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (ModelObject shield in _levelShields)
            {
                shield.Draw(gameTime);
            }
            base.Draw(gameTime);
        }
    }
}
